package com.duolaibei.pay.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.duolaibei.pay.R;
import com.duolaibei.pay.base.BaseActivity;
import com.duolaibei.pay.base.Constants;
import com.duolaibei.pay.http.BaseCallback;
import com.duolaibei.pay.http.HttpRetrofit;
import com.duolaibei.pay.http.ParamsUtils;
import com.duolaibei.pay.http.Result;
import com.duolaibei.pay.http.api.ApiName;
import com.duolaibei.pay.http.api.SerivceApi;
import com.duolaibei.pay.utils.SharedPreferencesUtils;
import com.duolaibei.pay.utils.ToastUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by wangshuo on 2017/5/13.
 * 首页
 */

public class HomepageActivity extends BaseActivity implements BaseCallback<Result<Map<String, Object>>> {
    @Bind(R.id.arrow)
    TextView arrow;
    @Bind(R.id.collect_money_layout)
    RelativeLayout collectMoneyLayout;
    @Bind(R.id.setting_layout)
    RelativeLayout settingLayout;
    @Bind(R.id.merchant_information_layout)
    RelativeLayout merchantInformationLayout;
    @Bind(R.id.space)
    TextView space;
    @Bind(R.id.unionPay_layout)
    RelativeLayout unionPayLayout;

    final String TAG = "HomepageActivity";

    @Override
    public int getContentViewResId() {
        return R.layout.activity_homepage;
    }

    @Override
    public void intiViews() {
        HttpRetrofit.getInstance().getInterfaceApi(SerivceApi.class).login(
                SharedPreferencesUtils.getAppid(),
                ApiName.LOGIN,
                ParamsUtils.login(),
                SharedPreferencesUtils.getTerminalNumber(),
                SharedPreferencesUtils.getWorkMenId()
        ).enqueue(this);
        collectMoneyLayout.setOnClickListener(this);
        settingLayout.setOnClickListener(this);
        merchantInformationLayout.setOnClickListener(this);
        unionPayLayout.setOnClickListener(this);
    }

    @Override
    public String setTitle() {
        return "";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addCategory(Intent.CATEGORY_HOME);
            startActivity(intent);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id = view.getId();
        switch (id) {
            case R.id.collect_money_layout://收银
                Intent intent1 = new Intent(this, CollectMoneyActivity.class);
                startActivity(intent1);
                break;
            case R.id.setting_layout://设置
                Intent intent2 = new Intent(this, SettingActivity.class);
                startActivity(intent2);
                break;
            case R.id.merchant_information_layout://商户信息
                Intent intent3 = new Intent(this, MerchantInfoActivity.class);
                startActivity(intent3);
                break;
            case R.id.unionPay_layout://银联
                try {
                    sign();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }

    }

    private void sign() {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.shengpay.smartpos.shengpaysdk",
                "com.shengpay.smartpos.shengpaysdk.activity.MainActivity"));
        intent.putExtra("transName", "5");
        intent.putExtra("appId", getPackageName());
        startActivityForResult(intent, 0);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Bundle extras1 = data.getExtras();
        Set<String> keySet = extras1.keySet();
        for (String s : keySet) {
            Log.d(TAG, "onActivityResult: " + s + ":" + extras1.get(s));
        }
        Log.d(TAG, "onActivityResult: " + resultCode);

        switch (resultCode) {
            case Activity.RESULT_OK:
                Toast.makeText(HomepageActivity.this, "签到成功", Toast.LENGTH_SHORT).show();
                break;
            case Activity.RESULT_CANCELED:
                String reason = data.getStringExtra("reason");
                Toast.makeText(HomepageActivity.this, reason, Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    public void onResponse(Call<Result<Map<String, Object>>> call, Response<Result<Map<String, Object>>> response) {
        Log.v("-----", "------" + response.body());
        if (response.code() == 200) {
            if (response.body().getStatus().equals("1")) {
                Map<String, Object> result = response.body().getResult();
                Map<String, String> map = new HashMap<>();
                String shopName = ((Map<String, String>) result.get("mer")).get("real_name");
                String bankCardName = ((Map<String, String>) result.get("mer")).get("bank_account_name");
                String bankCard = ((Map<String, String>) result.get("mer")).get("bank_card");
                String url = ((Map<String, String>) result.get("terminal")).get("url");
                map.put(Constants.SHOP_NAME, shopName);
                map.put(Constants.BANK_ACOUNT_NAME, bankCardName);
                map.put(Constants.BANK_NUMBER, bankCard);
                map.put(Constants.URL, url);
                SharedPreferencesUtils.addMultipleToSharedPreferences(this, Constants.SHOP_INFO_SP_NAME, map);

            } else {
//                ToastUtils.showToast(this, response.body().getInfo(), 5000);
//                SharedPreferencesUtils.deleteAll(this, Constants.SHOP_INFO_SP_NAME);
//                SharedPreferencesUtils.deleteAll(this, Constants.APP_INFO_SP_NAME);
//                Intent intent = new Intent(this, MainActivity.class);
//                startActivity(intent);
//                finish();
            }
        }
    }

    @Override
    public void onFailure(Call<Result<Map<String, Object>>> call, Throwable t) {
        ToastUtils.showToast(this, getString(R.string.login_fail));
        Log.v("onFailure", "error message:" + t.getMessage());
    }
}
